package edu.umn.contactviewer;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;
import edu.umn.contactviewer.models.Contact;
import edu.umn.contactviewer.utils.FileOps;
import edu.umn.contactviewer.utils.JsonSerializer;

import java.util.ArrayList;
import java.util.Collections;

/**
 * Created by IntelliJ IDEA.
 * User: Socer3d
 * Date: 2/25/12
 * Time: 6:58 PM
 * To change this template use File | Settings | File Templates.
 */
public class ContactEditActivity extends Activity implements View.OnClickListener {
    private Contact contact;
    EditText nameField;
    EditText titleField;
    EditText phoneField;
    EditText emailField;
    EditText twitterField;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.contact_edit);
        ToolbarConfig toolbarConfig = new ToolbarConfig(this, "Edit Contact");
        toolbarConfig.getToolbarBackButton().setText( "Cancel" );
        toolbarConfig.getToolbarRightButton().setText( "Save" );

        Bundle b = this.getIntent().getExtras();
        contact = (Contact) b.getSerializable("contact");

        nameField = ((EditText)findViewById(R.id.contact_name));
        nameField.setText(this.contact.getName());
        titleField = ((EditText)findViewById(R.id.contact_title));
        titleField.setText(contact.getTitle());
        ((TextView)findViewById(R.id.twitter)).setText("Twitter ID:");
        twitterField = ((EditText)findViewById(R.id.contact_twitter));
        twitterField.setText(contact.getTwitterId());
        ((TextView)findViewById(R.id.email)).setText("Email:");
        emailField = ((EditText)findViewById(R.id.contact_email));
        emailField.setText(contact.getEmail());
        ((TextView)findViewById(R.id.phone)).setText("Phone:");
        phoneField = ((EditText)findViewById(R.id.contact_phone));
        phoneField.setText(contact.getPhone());

        findViewById(R.id.toolbar_right_button).setOnClickListener(this);
    }

    public void onClick(View view) {
        if (view.getId() == R.id.toolbar_right_button) {
            //get values
            contact.setName(nameField.getText().toString());
            contact.setTitle(titleField.getText().toString());
            contact.setEmail(emailField.getText().toString());
            contact.setPhone(phoneField.getText().toString());
            contact.setTwitterId(twitterField.getText().toString());

            //get existing contacts
            Context ctxt = this.getBaseContext();
            String rawJson = FileOps.readFile(ctxt);
            ArrayList<Contact> contacts = new ArrayList<Contact>();
            if(rawJson != null){
                contacts = JsonSerializer.parseContacts(rawJson);
                Collections.sort(contacts);
            }

            //check if this contact is overwriting an existing one or is completely new
            if(!contacts.isEmpty()) {
                Contact staleContact = null;
                for(Contact c : contacts) {
                    if(c.getName().equals(contact.getName())) {
                       staleContact = c;
                    }
                }
                //match was found
                if(staleContact != null) {
                    contacts.remove(staleContact);
                    contacts.add(contact);
                }
                //no match found
                else {
                    contacts.add(contact);
                }
            }
            else {
                contacts.add(contact);
            }

            //save the contact
            FileOps.writeFile(ctxt, JsonSerializer.toJSON(contacts));

            //go back to Info view
            Bundle bundle = new Bundle();
            bundle.putSerializable("contact", contact);
            Intent intent = new Intent(this, ContactInfoActivity.class);
            intent.putExtras(bundle);
            startActivity(intent);
            overridePendingTransition( R.anim.slide_in_right, R.anim.slide_out_right );

        }
    }
}
